
from kubernetes import client, config

class k8sDeploy:

    def create_deployment(apps_v1_api, app_name, images, port, label, match_label, deploy_name, ns, rs):
        container = client.V1Container(
            name=app_name,
            image=images,
            image_pull_policy="Always",
            ports=[client.V1ContainerPort(container_port=port)],
        )
        # Template
        template = client.V1PodTemplateSpec(
            metadata=client.V1ObjectMeta(labels={"app": label}),
            spec=client.V1PodSpec(containers=[container]))
        # Spec
        spec = client.V1DeploymentSpec(
            replicas=rs,
            selector=client.V1LabelSelector(
                match_labels={"app": match_label}
            ),
            template=template)
        # Deployment
        deployment = client.V1Deployment(
            api_version="apps/v1",
            kind="Deployment",
            metadata=client.V1ObjectMeta(name=deploy_name),
            spec=spec)
        # Creation of the Deployment in specified namespace
        # (Can replace "default" with a namespace you may have created)
        apps_v1_api.create_namespaced_deployment(
            namespace=ns, body=deployment
        )
        return "CREADTE DONE!"

    def create_service(service_name,app_name,port,container_port):
        core_v1_api = client.CoreV1Api()
        body = client.V1Service(
            api_version="v1",
            kind="Service",
            metadata=client.V1ObjectMeta(
                name=service_name
            ),
            spec=client.V1ServiceSpec(
                selector={"app": app_name},
                ports=[client.V1ServicePort(
                    port=port,
                    target_port=container_port
                )]
            )
        )
        # Creation of the Deployment in specified namespace
        # (Can replace "default" with a namespace you may have created)
        core_v1_api.create_namespaced_service(namespace="blc", body=body)
        return "CREADTE DONE!"

    def create_ingress(networking_v1_api,ing_name,ns,domain,prefix,numbers,service_name):
        body = client.V1Ingress(
            api_version="networking.k8s.io/v1",
            kind="Ingress",
            metadata=client.V1ObjectMeta(name=ing_name, annotations={
                "nginx.ingress.kubernetes.io/rewrite-target": "/"
            }),
            spec=client.V1IngressSpec(
                rules=[client.V1IngressRule(
                    host=domain,
                    http=client.V1HTTPIngressRuleValue(
                        paths=[client.V1HTTPIngressPath(
                            path=prefix,
                            path_type="Exact",
                            backend=client.V1IngressBackend(
                                service=client.V1IngressServiceBackend(
                                    port=client.V1ServiceBackendPort(
                                        number=numbers,
                                    ),
                                    name=service_name)
                                )
                        )]
                    )
                )
                ]
            )
        )
        # Creation of the Deployment in specified namespace
        # (Can replace "default" with a namespace you may have created)
        networking_v1_api.create_namespaced_ingress(
            namespace=ns,
            body=body
        )
        return "CREADTE DONE!"


if __name__ == "__main__":
    config.load_kube_config(config_file='/root/.kube/config')
    apps_v1_api = client.AppsV1Api()
    networking_v1_api = client.NetworkingV1Api()
    deply=k8sDeploy()
    deply.create_deployment()
    create_deployment(apps_v1_api)
    create_service()
    creat